/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.commands;

import core.DispatcherPort;
import core.InPort;
import core.Node;
import core.TriggerPort;
import model.graphic.objects.GConnection;
import model.graphic.objects.GDispatcherPort;
import model.graphic.objects.GHandleConnection;
import model.graphic.objects.GInputPort;
import model.graphic.objects.GNode;
import model.graphic.objects.GOutput;
import model.graphic.objects.GOutputPort;
import model.graphic.objects.GTrigger;
import model.graphic.objects.GTriggerConnection;
import model.graphic.objects.GTriggerPort;
import model.graphic.objects.RectangleGraphical;
import view.GUI.LabelReader;

/**
 *
 * @author Jelle
 */
public class DetachConnectionCommand extends BasicCommand {

    private GInputPort inputport;
    private GDispatcherPort dispatcherport;
    private GTrigger trigger;
    private GOutput triangle;
    private int modus = 0;
    private GConnection c;

    public DetachConnectionCommand(GConnection c) {
        this.c = c;
        if (GTriggerConnection.class.isAssignableFrom(c.getClass())) {
            GTriggerConnection gc = (GTriggerConnection) c;
            this.dispatcherport = (GDispatcherPort) gc.getDispatcher();
            this.trigger = (GTrigger) gc.getTrigger();
            modus = 1;

        } else if (GHandleConnection.class.isAssignableFrom(c.getClass())) {
            GHandleConnection gc = (GHandleConnection) c;
            this.inputport = (GInputPort) gc.getInput();
            if(GOutput.class.isAssignableFrom(gc.getOutput().getClass())){
                this.triangle = (GOutput) gc.getOutput();
            }
            modus = 2;
        }
    }

    @Override
    public void execute() {
        if (modus == 1) {

            RectangleGraphical g1 = (RectangleGraphical) dispatcherport.getBase();
            RectangleGraphical g2 = (RectangleGraphical) ((GTriggerPort) trigger.getBase()).getBase();
            Node n1 = sync.get(g1);
            Node n2 = sync.get(g2);
            GNode gn1 = (GNode) g1;
            GNode gn2 = (GNode) g2;
            gn1.removeConnectionToNode(c);
            gn2.removeConnectionToNode(c);
            try {
                ((DispatcherPort) n1.getPort(dispatcherport.getName())).removeTarget((TriggerPort) n2.getPort(trigger.getBase().getName()));
            } catch (Exception ex) {
                System.out.println(LabelReader.getInstance().getString("EXCEPTIONdeConnectionCommand") + "1111" + ex.getMessage());
            }
        } else if (modus == 2) {
            RectangleGraphical g2 = (RectangleGraphical) inputport.getBase();
            Node n2 = sync.get(g2);
            GNode gn2 = (GNode) g2;
            gn2.removeConnectionToNode(c);
            try {
                RectangleGraphical g1 = (RectangleGraphical) ((GOutputPort) triangle.getBase()).getBase();
                GNode gn1 = (GNode) g1;
                gn1.removeConnectionToNode(c);
                ((InPort) n2.getPort(inputport.getName())).setSource(null);
            } catch (Exception ex) {
                System.out.println(LabelReader.getInstance().getString("EXCEPTIONdeConnectionCommand") +"222222" + ex.getMessage());
            }
        }
        controller.getDebug().append(LabelReader.getInstance().getString("ConnectionBetweenPortsRemoved"));
    }
}